//数据库配置
interface DbConConf {
  host: string;
  user: string;
  password: string;
  port: number;
  database: string;
}
// 环境配置
interface EnvConf {
  dev: DbConConf;
  prod: DbConConf;
}
class Conf {
  static conf: Conf = new Conf();
  env!: keyof EnvConf;
  envConf!: EnvConf;
  constructor() {
    this.env = process.env.NODE_ENV === "dev" ? "dev" : "prod";
    this.initConf();
  }
  // 初始化数据配置
  initConf() {
    this.envConf = {
      dev: {
        host: "localhost",
        user: "root",
        password: "12345678",
        port: 3306,
        database: "admin", // 数据库名称,
      },
      prod: {
        host: "localhost",
        user: "root",
        password: "12345678",
        port: 3306,
        database: "admin",
      },
    };
  }
  // 函数重载
  getConf(): DbConConf;
  getConf(key: string): string;
  getConf(key: string = ""): any {
    if (this.isDbConConfkeys(key) && key.length > 0) {
      return this.envConf[this.env][key];
    } else {
      return this.envConf[this.env];
    }
  }

  isDbConConfkeys(key: any): key is keyof DbConConf {
    return (
      key === "host" ||
      key === "user" ||
      key === "password" ||
      key === "port" ||
      key === "database"
    );
  }
}

export default Conf.conf
